package database;

import gui.ExceptionEvent;
import gui.ExceptionListener;

import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.event.EventListenerList;

public class DataSuperclass {

	protected PreparedStatement ps = null;
	protected EventListenerList listenerList = new EventListenerList();
	protected Connection con = null;

	public DataSuperclass() {
		super();
	}

	public Connection getConnection() {
		return con;
	}

	protected void finalize() throws Throwable {
		if (ps != null) {
			ps.close();
		}
		super.finalize();
	}

	public void addExceptionListener(ExceptionListener l) {
		listenerList.add(ExceptionListener.class, l);
	}

	public void removeExceptionListener(ExceptionListener l) {
		listenerList.remove(ExceptionListener.class, l);
	}

	public void fireExceptionGenerated(ExceptionEvent ex) {
		Object[] listeners = listenerList.getListenerList();
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == ExceptionListener.class) {
				((ExceptionListener) listeners[i + 1]).exceptionGenerated(ex);
			}
		}
	}

}